﻿using Spire.Xls;
using System;
using System.Collections.Generic;
using System.Configuration;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ExcelAutoPrint
{
    class Program
    {
        static void Main(string[] args)
        {
            try
            {

                string storeid = ConfigurationManager.AppSettings["StoreID"];
                FreeADO.Common.WebClient wc = new FreeADO.Common.WebClient();
                wc.Encoding = Encoding.UTF8;
                string s = wc.GetHtml("http://test.waquan.com.cn/writ/wjapp.php/order/getWaitOrderExcel?store_id=" + storeid);
                //s = "{\"code\":200,\"data\":{\"list\":[\"http:\\/\\/shopwaquan.oss-cn-beijing.aliyuncs.com\\/app\\/excel\\/order\\/20220819\\/a61dcb89d8140e328da665e42e8397cd3132eaf7.xlsx\",\"http:\\/\\/shopwaquan.oss-cn-beijing.aliyuncs.com\\/app\\/excel\\/order\\/20220819\\/f3e9a38e197df5dd4d7785d021f7460d57280a1e.xlsx\",\"http:\\/\\/shopwaquan.oss-cn-beijing.aliyuncs.com\\/app\\/excel\\/order\\/20220819\\/6569b852d4c2b813fd4b30fd0d92c534599001e4.xlsx\"],\"count\":3},\"msg\":\"\u64cd\u4f5c\u6210\u529f\",\"count\":0}";
                //s = "{\"code\":200,\"data\":{\"list\":[\"http:\\/\\/shopwaquan.oss-cn-beijing.aliyuncs.com\\/app\\/excel\\/order\\/20220819\\/a61dcb89d8140e328da665e42e8397cd3132eaf7.xlsx\"],\"count\":1},\"msg\":\"\u64cd\u4f5c\u6210\u529f\",\"count\":0}";
                Result m = Newtonsoft.Json.JsonConvert.DeserializeObject<Result>(s);

                foreach (string item in m.data.list)
                {
                    try
                    {
                        string path = Directory.GetCurrentDirectory() + "\\DownLoadFile";
                        if (!Directory.Exists(path))
                        {
                            Directory.CreateDirectory(path);
                        }
                        string _filename = item.Split(new char[] { '/' })[item.Split(new char[] { '/' }).Length - 1];
                        string fileName = path + "\\" + _filename;
                        wc.DownloadFile(item, fileName);
                        Workbook workbook = new Workbook();
                        workbook.LoadFromFile(fileName);
                        workbook.Worksheets[0].PageSetup.Orientation = PageOrientationType.Landscape;
                        workbook.PrintDocument.Print();
                        File.Delete(fileName);
                    }
                    catch
                    {

                    }
                }
            }
            catch (Exception e)
            {

            }
        }
    }
    public class Data
    {
        /// <summary>
        /// 
        /// </summary>
        public List<string> list { get; set; }
        /// <summary>
        /// 
        /// </summary>
        public int count { get; set; }
    }

    public class Result
    {
        /// <summary>
        /// 
        /// </summary>
        public int code { get; set; }
        /// <summary>
        /// 
        /// </summary>
        public Data data { get; set; }
        /// <summary>
        /// 操作成功
        /// </summary>
        public string msg { get; set; }
        /// <summary>
        /// 
        /// </summary>
        public int count { get; set; }
    }
}
